package cn.seapon.talker.adapters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.seapon.talker.ChatActivity;
import cn.seapon.talker.R;
import cn.seapon.talker.bean.MessageEntity;
import cn.seapon.talker.common.FriendListInfomation;
import cn.seapon.talker.infocarrier.SharedItems;

/**
 * 聊天列表的数据适配器
 * 
 * @author Asuron Created on 2014年12月31日 下午3:08:30
 */
public class ChatMsgListAdapter extends BaseAdapter {
	private static final String TAG = "||__ChatMessageListAdapter__||";

	private Context context;
	private List<MessageEntity> chatMsgEntities;
	private LayoutInflater inflater;
	// 表情的宽度和高度的资源表示
	private int expression_size = -1;

	public ChatMsgListAdapter(List<MessageEntity> chatMsgEntities,
			Context context) {
		this.chatMsgEntities = chatMsgEntities;
		this.context = context;
		inflater = LayoutInflater.from(context);
		expression_size = (int) this.context.getResources().getDimension(
				R.dimen.chat_expression_size);
	}

	public void setL(List<MessageEntity> chatMsgEntities) {
		this.chatMsgEntities = chatMsgEntities;
	}

	public int getCount() {
		return this.chatMsgEntities.size();
	}

	public Object getItem(int position) {
		return this.chatMsgEntities.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	// 从HTML标签中取回字符串
	final Html.ImageGetter imageGetter_resource = new Html.ImageGetter() {
		public Drawable getDrawable(String source) {
			Drawable drawable = null;
			int resourceId = Integer.parseInt(source);
			drawable = context.getResources().getDrawable(resourceId);
			// x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的
			drawable.setBounds(0, 0, expression_size, expression_size);// 设置显示的图像大小
			return drawable;
		};
	};

	/**
	 * 格式化空格数据
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceSpaceToCode(String str) {
		String rt = str.replace(" ", "&nbsp;");
		rt = rt.replace("\n", "<br/>");
		return rt;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		MessageEntity chatMessage = chatMsgEntities.get(position);
		ViewHolder holder = null;
		// Log.e(TAG, chatMessage.getJSONString());
		String account = SharedItems.userInformation.getAccount();
		String receiver = chatMessage.getReceiver();
		String sender = chatMessage.getSender();
		long posttime = chatMessage.getPosttime();
		DateFormat dateFormat = new SimpleDateFormat("hh:mm");
		Log.e(TAG, "account:" + account + "   receiver:" + receiver
				+ "  sender:" + sender);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		boolean ismine;
		// if(null==convertView){
		holder = new ViewHolder();
		if (account.equals(sender)) {// 发送者是自己
			ismine = true;
			convertView = inflater.inflate(
					R.layout.chat_msg_listview_item_mine, null);
		} else {
			ismine = false;
			convertView = inflater.inflate(
					R.layout.chat_msg_listview_item_friend, null);
		}
		holder.tv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
		holder.tv_content = (TextView) convertView
				.findViewById(R.id.tv_content);
		holder.tv_nickname = (TextView) convertView
				.findViewById(R.id.tv_nickname);
		makeAvatar(holder, sender, ismine);
		holder.tv_nickname.setText(dateFormat.format(posttime));
		convertView.setTag(holder);
		// }else{
		// holder = (ViewHolder)convertView.getTag();
		// }
		holder.tv_content.setText(Html.fromHtml(
				msgConvert(replaceSpaceToCode(chatMessage.getContent())),
				imageGetter_resource, null));
		// String content =
		// msgConvert(replaceSpaceToCode(chatMessage.getContent()));
		// Log.v("_____________", "3content = " + content);
		return convertView;
	}

	private void makeAvatar(ViewHolder holder, String sender, boolean ismine) {
		if (ismine) {
			// 旧版兼容
			if ("".equals(SharedItems.userInformation.getAvatar())
					|| "null".equals(SharedItems.userInformation.getAvatar())) {
				holder.tv_logo.setBackgroundResource(R.drawable.header);
			} else {
				try {
					holder.tv_logo.setBackgroundResource(SharedItems.avatarMap
							.get(SharedItems.userInformation.getAvatar()));
				} catch (Exception e) {
					holder.tv_logo.setBackgroundResource(R.drawable.header);
				}
			}
		} else {
			if ("".equals(SharedItems.userInformation.getAvatar())
					|| "null".equals(SharedItems.userInformation.getAvatar())) {
				holder.tv_logo.setBackgroundResource(R.drawable.header);
			} else {
				try {
					holder.tv_logo.setBackgroundResource(SharedItems.avatarMap
							.get(SharedItems.friendListInfomation.getInfoById(
									sender).getAvatar()));
				} catch (Exception e) {
					holder.tv_logo.setBackgroundResource(R.drawable.header);
				}
			}

		}
	}

	private String msgConvert(String content) {
		Log.v("_____________", "转换前  content = " + content);
		for (int i = 0; i < ChatActivity.expressionList.size(); i++) {
			content = content.replace(ChatActivity.expressionList.get(i)
					.getCode(), "<img src=\""
					+ ChatActivity.expressionList.get(i).getDrableId()
					+ "\" />");
		}
		Log.v("_____________", "转换后  content = " + content);
		return content;
	}

	private class ViewHolder {
		public ImageView tv_logo;
		public TextView tv_nickname;
		public TextView tv_content;
	}
}